// @ts-check
/**
 * 公用的方法
 */
import { fetch, post } from './requests'
import { commonService, carService, accountService } from './env'
import { getLocalStorage } from './localStorage'
import { Message } from 'element-ui'

/**
 * 获取主键
 * @returns { Promise<{ code: string, message: string, data: string }> } 返回值：主键值
 */
const getPrimaryKey = (num) => {
  const data = {}
  if (num) {
    data.count = num
  }
  const url = commonService + '/api/common/get_primary_key'
  return fetch(url, data, {}, true)
}

/**
 * 写操作日志
 * @param { { id: string, staff_id: string, log_type: string, content: string, meta: string } } data 请求参数
 * @param {object} headers 请求头
 */
const _writeOperationLog = (data, headers) => {
  const url = accountService + '/data/staff_log'
  return post(url, data, headers, true)
}

const primaryKey = async () => {
  const keyRes = await getPrimaryKey()
  if (!keyRes) {
    Message.error('生成主键失败！')
    return false
  }
  return keyRes.data
}

const writeLogger = async data => {
  const staffId = getLocalStorage('sps_id') || ''
  data.staff_id = staffId
  const res = await _writeOperationLog(data, { 'Content-Type': 'application/json' })
  if (!res) {
    return false
  }
  return true
}

/**
 * 获取腾讯云cos
 * @description 获取腾讯云cos
 * @returns { Promise<> } 返回值：完整的响应体信息
 */
const getKey = () => {
  const url = commonService + '/api/txcos/key'
  return post(url, {}, {}, true)
}

/**
 * 获取城市信息
 * @description 获取获取城市信息
 * @returns { Promise<> } 返回值：完整的响应体信息
 */
const feachCity = () => {
  const url = carService + '/api/car_city'
  return fetch(url, {}, {}, true)
}

/**
 * 获取车辆品牌信息
 * @description 获取车辆品牌信息
 * @returns { Promise<> } 返回值：完整的响应体信息
 */
const feachBrand = () => {
  const url = carService + '/api/car_brand'
  return fetch(url, {}, {}, true)
}

export {
  getPrimaryKey,
  primaryKey,
  writeLogger,
  getKey,
  feachCity,
  feachBrand
}
